package com.mysen.sso.conf;
/**
 * springboot中,继承为springmvc提供的WebMvcConfigurerAdapter , 其相当于 springmvc的springmvc.xml配置
 */

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.mysen.sso.PermissionCheck;
import com.mysen.sso.cache.SessionCache;

@ConfigurationProperties
@Configuration
public class SpringmvcWebconfig implements WebMvcConfigurer{

	@Value("${spring.mvc.permission.paths}")
	private String[] paths;
	
	@Autowired
	private SessionCache sessionCache;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	PermissionCheck check = new PermissionCheck();
    	check.setPass(paths);
    	check.setSessionCache(sessionCache);
        registry.addInterceptor(check);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	WebMvcConfigurer.super.addResourceHandlers(registry);
    	registry.addResourceHandler("/static/**");
    }
    
	@Bean
	public FastJsonConfig fastJsonConfig() {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		return fastJsonConfig;
	}
	
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters(
			@Qualifier("fastJsonConfig") FastJsonConfig fastJsonConfig) {
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		fastConverter.setFastJsonConfig(fastJsonConfig);
		fastConverter.setSupportedMediaTypes(new ArrayList<MediaType>() {
			private static final long serialVersionUID = 1L;
		{
			add(MediaType.APPLICATION_JSON_UTF8);
		}});
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
	
	@Bean
	public CommonsMultipartResolver commonsMultipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(104857600);
		multipartResolver.setMaxInMemorySize(4096);
		multipartResolver.setDefaultEncoding("UTF-8");
		return multipartResolver;
	}
	
	@Bean
	public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver() {
		return new ExceptionHandlerExceptionResolver();
	}
}